package com.yixiu.study.juc.exchanger;

import java.util.concurrent.Exchanger;

/**
 * @CalssName T01_Exchanger
 * @Description 只能两两交换
 * @Author yixiu
 * @Date 2021/6/25 10:12
 * @Verison num1.0
 **/
public class T01_Exchanger {

    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(() -> {
            String s = "T1";
            try {
                s = exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);

        }, "t1").start();

        new Thread(() -> {
            String s = "T2";
            try {
                s = exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);

        }, "t2").start();

    }


}
